// 从cookie中获取存起来的用户名
var username = getCookie('username')
// 判断
if(username){
    $('.fl ul').html(`
        <li>欢迎<a href="userinfo.html" style="color:skyblue;font-size:16px;padding: 0 5px;" class="ma5">${username}</a>登录！</li>
        <li><a class="red" onclick="logout()" style="color: red;">退出登录</a></li>
    `)
}
// 定义退出函数
function logout(){
    // 询问层
    layer.confirm('你确定要退出吗？', {
        btn: ['残忍离开', '考虑一下']
    }, function(){
        // 将内容还原
        $('.fl ul').html(`
        <li><a href="login.html" class="ma5">登录</a></li>
        <li><a class="red" href="register.html" style="color: skyblue;">免费注册</a></li>
        `)
        // 删除cookie
        removeCookie('username')
        removeCookie('userId')
        localStorage.removeItem('token')
        layer.msg('退出成功', {
            icon: 1,
            time: 1500
        })
    }, function(){
        layer.msg('取消成功', {
            icon: 1,
            time: 1500
        })
    })
}






    // //欢迎提示
    // var lis = document.querySelectorAll('.fl li')
    // var arr = decodeURI(location.search.split('=')[1])
    // if (arr !== 'undefined') {
    //     lis[0].innerHTML = arr + ' 您好，品优购欢迎您！'
    //     lis[1].innerHTML = null
    // } else {
    //     lis[0].innerHTML = '<li>品优购欢迎您！</li>'
    //     lis[1].innerHTML = '<a href="login.html">请登录</a> <a href = "register.html" class = "style_red" > 免费注册 </a>'
    // }


    //search聚焦
    var search_text = document.querySelector('.search_text')
    document.addEventListener('keyup', function(e) {
        if (e.keyCode === 83) {
            search_text.value = null
            search_text.focus()
        }
    })


    //倒计时
    var count = document.querySelector('.count')
    var hour = document.querySelector('.hour')
    var minute = document.querySelector('.minute')
    var second = document.querySelector('.second')
    var inputTime = new Date('2022-12-24 16:00:00')
    getTime()
    var timer = setInterval(getTime, 1000)

    function getTime() {
        var date = new Date()
        var chaTime = (inputTime - date) / 1000
        if (chaTime <= 0) {
            clearInterval(timer)
        }
        var h = parseInt(chaTime / 60 / 60 % 24)
        var m = parseInt(chaTime / 60 % 60)
        var s = parseInt(chaTime % 60)

        h = h < 10 ? '0' + h : h
        m = m < 10 ? '0' + m : m
        s = s < 10 ? '0' + s : s

        hour.innerHTML = h
        minute.innerHTML = m
        second.innerHTML = s
    }


    //轮播图按钮
    var arrow_l = document.querySelector('.arrow-l')
    var arrow_r = document.querySelector('.arrow-r')
    var banner = document.querySelector('.banner')

    banner.addEventListener('mouseenter', function() {
        arrow_l.style.display = 'block'
        arrow_r.style.display = 'block'
        clearInterval(timer)
    })
    banner.addEventListener('mouseleave', function() {
        arrow_l.style.display = 'none'
        arrow_r.style.display = 'none'
        timer = setInterval(nextPic, 2500)
    })


    //小圆点
    var ul = banner.querySelector('ul')
    var ol = banner.querySelector('.banner ol')
    for (i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li')
        ol.appendChild(li);
        li.setAttribute('index', i)
        li.addEventListener('click', function() {
            for (i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            this.className = 'current'
            index = this.getAttribute('index')
            animate(ul, -index * banner.offsetWidth)
        })
    }

    ol.children[0].className = 'current';
    var first = ul.children[0].cloneNode(true)
    ul.appendChild(first)
    var index = 0
    arrow_r.addEventListener('click', nextPic)

    var flag = true

    function nextPic() {
        if (flag == true) {
            flag = false
            if (index == ul.children.length - 1) {

                ul.style.left = 0
                index = 0
            }
            index++
            animate(ul, -index * banner.offsetWidth, function() {
                flag = true
            })

            for (i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            if (index == ul.children.length - 1) {
                ol.children[0].className = 'current'
            } else {
                ol.children[index].className = 'current'
            }

        }
    }

    var timer = setInterval(nextPic, 2500)

    arrow_l.addEventListener('click', function() {
        if (flag) {
            flag = false
            if (index == 0) {
                index = ul.children.length - 1
                ul.style.left = -index * banner.offsetWidth + 'px'
            }
            index--
            animate(ul, -index * banner.offsetWidth, function() {
                flag = true
            })
            for (i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            ol.children[index].className = 'current'
        }
    })
    //发送请求获取所有is_hot的商品并渲染到页面
var current = 1
var flag = true
var getdataFlag = true
getData()

function getData() {
    if (!flag) return false
    flag = false
    current++
    $.get('http://localhost:8888/goods/list', {
        pagesize: 10,
        current,
        filter: 'hot',


    }).then(res => {
        console.log(res);
        // var arr = res.list.filter(item => item.is_hot)
        // console.log(arr);
        if (current > 5) {
            getdataFlag = false
        }
        var html = ''
        res.list.forEach(item => {
            html += `
                <li>
                    <a href="shop_goods.html?id=${item.goods_id}">
                        <img src="${item.img_big_logo} " alt=" ">
                    </a>
                    <div class="goods_msg ">
                        <h4 class="googs_title two_row ">
                            ${item.title}
                        </h4>
                        <div class="price ">
                            <i>￥</i>${item.current_price}<i>00</i>
                        </div>
                        <div class="lookfor_sim ">
                            <div class="xiangsi ">找相似</div>
                        </div>
                    </div>
    
                </li>
                `
        });

        $('ul.for_you_goodslist').html($('ul.for_you_goodslist').html() + html)
        flag = true
    })

}

// 商品懒加载 - 监听滚动过的距离
window.onscroll = function () {
    if (getdataFlag) {
        // 获取滚动过的距离
        var w = document.documentElement.scrollTop || document.body.scrollTop
        var clientHeight = document.documentElement.clientHeight
        // 判断是否快到商品底部了
        if (w + clientHeight + 200 >= $('.wa').offset().top + $('.wa').height()) {
            getData()
        }
    }

}

    //电梯栏
    $(function() {
        var boxTop = $(".today").offset().top;
        var flag = true
        togoTool()

        function togoTool() {
            if ($(document).scrollTop() >= boxTop) {
                $(".fixedtool").fadeIn();
            } else {
                $(".fixedtool").fadeOut();
            }
        }
        $(window).scroll(function() {
            togoTool()
            if (flag) {
                var scrollTop = $(document).scrollTop()
                $('.floor .w').each(function(i, e) {
                    var wTop = $(e).offset().top
                    if (scrollTop > wTop - 20) {
                        $('.fixedtool ul li').eq(i).addClass('current').siblings().removeClass('current')
                    }
                })
            }

        })
        $('.fixedtool ul li').click(function() {
            flag = false
            var index = $(this).index()
            $(this).addClass('current').siblings('li').removeClass()

            var current = $('.floor .w').eq(index).offset().top
            $('body, html').stop().animate({
                scrollTop: current
            }, function() {
                flag = true
            })
        })
    })
